package com.example.xgakj.newterminal.ui.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.base.BaseActivity;
import com.example.xgakj.newterminal.base.BaseApplication;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.beans.CustomerTable;
import com.example.xgakj.newterminal.models.QueryModel;
import com.example.xgakj.newterminal.ui.dialogs.UpdateDialog;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserInfoActivity extends BaseActivity {
    String userId;
    String ssn;
    @BindView(R.id.btn_update)
    Button btnUpdate;
    @BindView(R.id.tv_ID)
    TextView tvID;
    @BindView(R.id.tv_nickName)
    EditText tvNickName;
    @BindView(R.id.tv_areaName)
    EditText tvAreaName;
    @BindView(R.id.tv_defence)
    TextView tvDefence;
    @BindView(R.id.tv_talkTime)
    TextView tvTalkTime;
    @BindView(R.id.tv_guardTel)
    EditText tvGuardTel;
    @BindView(R.id.tv_110Tel)
    EditText tv110Tel;
    @BindView(R.id.tv_address)
    EditText tvAddress;
    @BindView(R.id.tv_endTime)
    TextView tvEndTime;
    @BindView(R.id.btn_update_password)
    Button btnUpdatePassword;
    @BindView(R.id.tv_note)
    EditText tvNote;
    @BindView(R.id.tv_xunluoTel)
    EditText tvXunluoTel;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        ssn = BaseApplication.ssn;
        userId = BaseApplication.usrID;
        QueryModel.getInstance().queryUserTable(ssn, userId);
        initTopBar();
    }

    private void initTopBar() {
        topbar.setTitle("用户资料");
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 根据查询结果刷新界面
     *
     * @param customerTable
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void freshViews(CustomerTable customerTable) {
        tvID.setText(customerTable.getCuid());
        tvNickName.setText(customerTable.getName());
        tvAreaName.setText(customerTable.getPartid());
        tvDefence.setText(checkDefence(customerTable.getStatus()));
        tvTalkTime.setText(customerTable.getRecvtime());
        tvGuardTel.setText(customerTable.getBaoan());
        tv110Tel.setText(customerTable.getPolice());
        tvAddress.setText(customerTable.getAddress());
        tvNote.setText(customerTable.getNote());
        tvEndTime.setText(customerTable.getDateend());
        tvXunluoTel.setText(customerTable.getXunluo());

    }


    private String checkDefence(int status) {
        String defence;
        switch (status) {
            case 0:
                defence = "已布防";
                break;
            case 1:
                defence = "已撤防";
                break;
            case 2:
                defence = "不在线";
                break;
            default:
                defence = "";
        }
        return defence;
    }

    @OnClick({R.id.btn_update, R.id.btn_update_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_update:
                showUpdate();
                break;
            case R.id.btn_update_password:
                showPasswordDialog();
                break;
            default:
        }

    }

    private void showPasswordDialog() {
        UpdateDialog updateDialog = new UpdateDialog();
        updateDialog.show(getSupportFragmentManager(), "updateDialog");
    }


    private void showUpdate() {
        new QMUIDialog.MessageDialogBuilder(this)
                .setTitle("修改用户信息")
                .setMessage("确定修改吗？")
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        String fname = "name,partid,baoan,xunluo,police,address,note";
                        String fvalue = getValues();
                        String cuid = BaseApplication.usrID;
                        QueryModel.getInstance().updateInfo(Parameters.CUSTOMER, "cuid", cuid, fname, fvalue, null);
                    }
                })
                .show();
    }


    private String getValues() {
        String fvalue = "'"
                + tvNickName.getText().toString()
                + "','" + tvAreaName.getText().toString()
                + "','" + tvGuardTel.getText().toString()
                + "','" + tvXunluoTel.getText().toString()
                + "','" + tv110Tel.getText().toString()
                + "','" + tvAddress.getText().toString()
                + "','" + tvNote.getText().toString()
                + "'";
        return fvalue;


    }


    public void onViewClicked2() {
//        ToastUtil.showToast(getApplicationContext(),"点击了修改密码");
//        UpdatePasswordDialog updatePasswordDialog = new UpdatePasswordDialog();
//        updatePasswordDialog.show(getSupportFragmentManager(), "修改密码");
    }


}
